package com.project;


import com.project.bean.RootBean;
import com.project.config.WebApplicationConfig;
import com.project.filter.*;
import com.project.service.IRootService;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.List;

public class MainServer7 {
//    private  WebApplicationContext ctx;

    public MainServer7() {
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8088);
        tomcat.getConnector();
        Context context = tomcat.addContext("",null);
//      注册过滤器
        this.addFilter(context,new CharacterEncodingFilter("utf-8",true),"/*");
        this.addFilter(context, new SupplierFilter("供应商管理", "/html/login.html?info=not"),"/html/count/saleCount.html");
        this.addFilter(context, new CustomerrFilter("客户管理", "/html/login.html?info=not"),"/html/ziliao/supplier/supplier.html");
        this.addFilter(context, new PutInFilter("采购入库", "/html/login.html?info=not"),"/html/buy/purchaseIn.html");
        this.addFilter(context, new ProcurementFilter("采购信息查询", "/html/login.html?info=not"),"/html/buy/purchaseFind.html");
        this.addFilter(context, new OutFilter("销售出库", "/html/login.html?info=not"),"/html/sell/salesOfOutBound.html");
        this.addFilter(context, new SellInfoFilter("销售信息查询", "/html/login.html?info=not"),"/html/sell/salesInfo.html");
        this.addFilter(context, new SellContFilter("销售统计", "/html/login.html?info=not"),"/html/count/saleCount.html");
        this.addFilter(context, new InventoryFilter("库存管理", "/html/login.html?info=not"),"/html/buy/stock.html");
        this.addFilter(context, new CusContFilter("客户统计", "/html/login.html?info=not"),"/html/count/clientCount.html");
        this.addFilter(context, new UserFilter("用户管理", "/html/login.html?info=not"),"/html/roleMent/users.html");
        this.addFilter(context, new RoleFilter("角色管理", "/html/login.html?info=not"),"/html/roleMent/role.html");
        this.addFilter(context, new NoteFilter("日志一栏", "/html/login.html?info=not"),"/html/note/note.html");


//        ctx=this.createApplicationContext(context.getServletContext());

//        this.addRootFilter(context,ctx);


        try {
//            //注册前端控制器
//            DispatcherServlet dispatcherServlet =new DispatcherServlet(
//                    this.createApplicationContext(context.getServletContext()));
            //注册前端控制器
            DispatcherServlet dispatcherServlet =new DispatcherServlet(
                    this.createApplicationContext(context.getServletContext()));
//            //注册前端控制器
//            DispatcherServlet dispatcherServlet =new DispatcherServlet(
//                    ctx);
            Wrapper servlet = tomcat.addServlet(context,"dispatcherServlet",dispatcherServlet);
            servlet.setLoadOnStartup(1);
            servlet.addMapping("/*");
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }

    /**
     * 创建spring容器的应用上下文对象
     * @param servletContext WEB容器上下文对象
     * @return sping容器应用上下文对象
     */
    public static WebApplicationContext createApplicationContext(
            ServletContext servletContext) {
        AnnotationConfigWebApplicationContext ctx
                = new AnnotationConfigWebApplicationContext();
        ctx.register(WebApplicationConfig.class);

        ctx.setServletContext(servletContext);
        ctx.refresh();
        ctx.registerShutdownHook();
        return ctx;
    }

    public void addFilter(Context context, Filter filterObj, String...filterPath){
        //注册过滤器
        FilterDef filterDef = new FilterDef();
        filterDef.setFilter(filterObj); //设置过滤器对象
        filterDef.setFilterName(filterObj.getClass().getSimpleName());//设置过滤器名称

        FilterMap filterMap = new FilterMap();
        for(String str:filterPath){
            filterMap.addURLPatternDecoded(str);
        }
        filterMap.setFilterName(filterObj.getClass().getSimpleName()); //该过滤范围应用的过滤器
        //注册
        context.addFilterDef(filterDef);
        context.addFilterMap(filterMap);
    }

//    /**
//     * 循环注册权限对应的过滤器
//     */
//    public void addRootFilter(Context context,WebApplicationContext ctx){
//        System.out.println("进入添加方法");
//        IRootService service = ctx.getBean(IRootService.class);
//        List<RootBean> rootList = service.findAll();
//        List<BaseFilter> filterList = new ArrayList<>();
//        for (RootBean r: rootList) {
//            System.out.println("开始注册");
//            System.out.println(r.getName());
//            System.out.println(r.getFilterPath());
//            this.addFilter(context, new BaseFilter(r.getName(), "/html/login.html?info=not"),r.getFilterPath());
//            System.out.println(r);
//            filterList.add(new BaseFilter(r.getName(), "/html/login.html?info=not"));
//        }
//        System.out.println(filterList);
//    }

    public static void main(String[] args){
        new MainServer7();
    }
}
